/*
 * Created on Feb 12, 2004
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package dbsucxentW.storageEdgeW.entitiesW;

import java.io.Serializable;

/**
 * @author Administrator
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class LeafNode implements Serializable{
	
	protected int _subtreeId; //Assuming no more than 2^31 - 1 level 2 subtrees.
	
	protected int _order;
	protected int _ordinal;
	//protected long _branchOrderSum;
	
	//extend BS
	protected boolean _isLeaf;
	protected boolean _isAttribute;		//klarinda 20060828
	protected boolean _isLargeTextData = false;	//klarinda 20070525 - add TextContent
	
	protected int _descendantId;
	protected int _parentId;
	protected int _depth;
	protected int _docId = -1;//1; //klarinda 20060905 hardcode docId for experiment purpose
	
	//protected String _path;
	protected int _pathId;
	
	protected String _value;
	protected StringBuilder _valueBuilder = new StringBuilder();
	
	//private static int LEAF_VALUE_SIZE = 50;
	private static int LEAF_VALUE_SIZE = 8000;

	//k 20070525
	public int getDocId() {
		return _docId;
	}
	public void setDocId(int docId) {
		_docId = docId;
	}

	
	/**
	 * @return
	 */
	public int getOrdinal() {
		return _ordinal;
	}

	/**
	 * @param branchOrder
	 */
	public void setOrdinal(int ordinal) {
		_ordinal= ordinal;
	}


	public void setIsLeaf(boolean isLeaf) {
		_isLeaf = isLeaf;	
	}
	
	public boolean getIsLeaf() {
		return _isLeaf;	
	}

	//klarinda 20060828
	public void setIsAttribute(boolean isAttr) {
		_isAttribute = isAttr;	
	}
	//klarinda 20060828
	public boolean getIsAttribute() {
		return _isAttribute;	
	}
	
	//klarinda 20070525 - add TextContent table
	public void setIsLargeTextData(boolean tc) {
		_isLargeTextData = tc;	
	}
	//klarinda 20070525
	public boolean getIsLargeTextData() {
		return _isLargeTextData;	
	}
	
	public void setDepth(int depth) {
		_depth = depth;	
	}
	
	public int getDepth() {
		return _depth;	
	}
	
	public void setDescendantId(int id){
		_descendantId = id; 
	}
	
	public int getDescendantId(){
		return _descendantId;
	}
	
	public void setParentId(int id){
		_parentId = id; 
	}
	
	public int getParentId(){
		return _parentId;
	}
	
	
	public LeafNode()
	{
		
	}
	/**
	 * @return
	 */
	public int getOrder()
	{
		return _order;
	}

	/**
	 * @param order
	 */
	public void setOrder(int order)
	{
		_order = order;
	}

	/**
	 * @return
	 */
	/*public String getPath()
	{
		return _path;
	}*/
	
	public int getPathId(){
		return _pathId;
	}

	/**
	 * @param path
	 */
	/*public void setPath(String path)
	{
		_path = path;
	}*/
	
	public void setPath( int pathId ){
		_pathId = pathId;
	}

	/**
	 * @return
	 */
	public String getValue()
	{
		//truncate();
		//return _value;
		return _valueBuilder.toString();
	}
	
	private void truncate(){
		if( _value.length() > LEAF_VALUE_SIZE ){
			_value = _value.substring( 0, LEAF_VALUE_SIZE - 1 );
		}
	}

	/**
	 * @param value
	 */
	public void setValue(String value)
	{
		//_value = value;
		//truncate();
		//escapeSql();
		_valueBuilder.append(value);
		escapeSql();
	}
	
	
	protected void escapeSql(){
		_value = _valueBuilder.toString();
		_value = _value.replace( "'", "''" );
		_valueBuilder = new StringBuilder();
		_valueBuilder.append(_value);
	}

	/**
	 * @return
	 */
	public int getSubtreeId() {
		return _subtreeId;
	}

	/**
	 * @param subtreeId
	 */
	public void setSubtreeId(int subtreeId) {
		_subtreeId= subtreeId;
	}

	//klarinda 20060905 bulk loading
	public String bulkLoad() {
		String sql;
		if(_isLargeTextData)	//k20070525 add TextContent
			sql = _docId + "\t" + _order + "\t" + _parentId + "\t" + _descendantId + "\t"
			+ _pathId + "\t" + "";
		else
			sql = _docId + "\t" + _order + "\t" + _parentId + "\t" + _descendantId + "\t"
			+ _pathId + "\t" + _valueBuilder.toString();
		return sql;
	}
	
	public String bulkLoadNonLeaf() {
		String sql;
		sql = _docId + "\t" + _order + "\t" + _parentId + "\t" + _descendantId + "\t"
			+ _pathId + "\t" + "null";
		return sql;
	}

	public String bulkLoadAttribute() {
		String sql;
		if(_isLargeTextData)	//k20070525 add TextContent
			sql = _docId + "\t" + _parentId + "\t" + _pathId + "\t" + "";
		else
			sql = _docId + "\t" + _parentId + "\t" + _pathId + "\t" + _valueBuilder.toString();
		return sql;
	}
	
	//return the string of element leaf node (k20070525 add TextContent)
	public String bulkLoadTextContent() {
		String sql;
		sql = _docId + "\t" + _order + "\t" + _parentId + "\t" + _descendantId + "\t"
			+ _pathId + "\t" + _valueBuilder.toString();
		return sql;
	}
	
	//return the string of attribute node (k20070525 add AttrContent)
	public String bulkLoadAttrContent() {
		String sql;
		sql = _docId + "\t" + _parentId + "\t" + _pathId + "\t" + _valueBuilder.toString();
		return sql;
	}
	

	//end of klarinda
}
